/**
 * 
 */
package com.vivid.dynamics.oknow.dao.impl;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vivid.dynamics.oknow.dao.IUserCategoryDAO;
import com.vivid.dynamics.oknow.model.UserCategory;

/**
 *
 * @author jason
 * created at:: Aug 12, 2011 3:54:37 PM
 */
public class UserCategoryDAOImpl implements IUserCategoryDAO {

	static Logger logger=Logger.getLogger(UserCategoryDAOImpl.class);
	
	private @Autowired SessionFactory sessionFactory;
	
	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IUserCategoryDAO#getUserCategoryByKey(int)
	 */
	@Override
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public UserCategory getUserCategoryByKey(int pId) {
		Session aSession=this.sessionFactory.getCurrentSession();
		UserCategory aCat=null;
		
		aCat=(UserCategory) aSession.get(UserCategory.class, Integer.valueOf(pId));
		
		return aCat;
	}

	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IUserCategoryDAO#newUserCateogry(com.vivid.dynamics.oknow.model.UserCategory)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public Serializable newUserCateogry(UserCategory pCategory) {
		Session aSession=this.sessionFactory.getCurrentSession();
		Serializable aKey=null;
		
		if (pCategory!=null) {
			aKey=aSession.save(pCategory);
			if (logger.isDebugEnabled()) {
				logger.debug("[newUserCategory] Key :: "+aKey);
			}
		}
		return aKey;
	}

	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IUserCategoryDAO#updateUserCategory(com.vivid.dynamics.oknow.model.UserCategory)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateUserCategory(UserCategory pCategory) {
		Session aSession=this.sessionFactory.getCurrentSession();
		
		if (pCategory!=null) {
			aSession.update(pCategory);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IUserCategoryDAO#deleteUserCategory(com.vivid.dynamics.oknow.model.UserCategory)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteUserCategory(UserCategory pCategory) {
		Session aSession=this.sessionFactory.getCurrentSession();

		if (pCategory!=null) {
			aSession.delete(pCategory);
		}
	}

}
